class Admin::UsersController < ApplicationController
  before_filter :authorize
  
  def add_user
    @user = User.new(params[:user])
    if request.post? and @user.save
      flash.now[:notice] = "Usu&aacute;rio #{@user.complete_name} criado com sucesso!<br/><br/>"
      @user = User.new
    end
    @departments = Department.find(:all, :order => :name).map {|c| [c.name, c.id]}
    @positions = Position.find(:all, :order => :hierarchy).map {|position| [position.name, 
          position.id]}
  end

  def delete_user
    if request.post?
      user = User.find(params[:id])
      begin
        user.destroy
        flash[:notice] = "Usu&aacute;rio #{user.name} exclu&iacute;do com sucesso!<br/><br/>"
      rescue Exception => e
        flash[:notice] = e.message
      end
    end
    redirect_to(:action => :list_users)
  end

  def list_users
    @all_users = User.find(:all)
  end
  
  def update_user
    @user = User.find(params[:id])
    if @user.update_attributes(params[:user])
      flash[:notice] = 'Usu&aacute;rio atualizado com sucesso!<br/><br/>'
      redirect_to :action => 'list_users'
    else
      flash[:notice] = 'Usu&aacute;rio n&atilde;o atualizado. Favor corrigir os erros.<br/><br/>'
      redirect_to :action => 'edit_user', :id => @user
    end
  end
  
  def edit_user
    @user = User.find(params[:id])
    @departments = Department.find(:all, :order => :name).map {|c| [c.name, c.id]}
    @positions = Position.find(:all, :order => :hierarchy).map {|position| [position.name, 
        position.id]}
  end
  
  def index
    redirect_to :action => 'list_users'
  end
  
end
